//
//  TestFMDB.m
//  ILGDemo
//
//  Created by zhangtongle-Pro on 2018/6/11.
//  Copyright © 2018年 Happy. All rights reserved.
//

#import "TestFMDB.h"

#import "ILGFMDBManager.h"

static NSString * const kIMMsgItemTableName = @"IMMessages";

@implementation TestFMDB
{
    NSString *_privateStr;
}

static TestFMDB *_instance = nil;
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone {
    return _instance;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    return _instance;
}

- (void)saveSessionContent:(NSString *)toUid msg:(NSString *)msg {
    NSString *createSql = [NSString stringWithFormat:@"create table IF NOT EXISTS %@ (id integer PRIMARY KEY AUTOINCREMENT, msg text, isMy integer);", [NSString stringWithFormat:@"%@%@", kIMMsgItemTableName, toUid]];
    if ([[ILGFMDBManager sharedInstance] executeUpdate:createSql]) {
        NSString *insertSql = [NSString stringWithFormat:@"insert into %@ (msg) values ('%@');", [NSString stringWithFormat:@"%@%@", kIMMsgItemTableName, toUid], msg];
        [[ILGFMDBManager sharedInstance] executeUpdate:insertSql];
        
    } else {
        
    }
}
- (void)saveMsgItem:(NSString *)str {
    NSString *createSql = [NSString stringWithFormat:@"create table IF NOT EXISTS %@ (id integer PRIMARY KEY AUTOINCREMENT, msg text);", kIMMsgItemTableName];
    if ([[ILGFMDBManager sharedInstance] executeUpdate:createSql]) {
        NSString *insertSql = [NSString stringWithFormat:@"insert into %@ (msg) values ('%@');", kIMMsgItemTableName, str];
        [[ILGFMDBManager sharedInstance] executeUpdate:insertSql];

    } else {
        
    }
}

- (NSArray *)messages {
    NSString *selectSql = [NSString stringWithFormat:@"select * from %@ order by id ASC limit 0,4;", [NSString stringWithFormat:@"%@200", kIMMsgItemTableName]];
    return [[ILGFMDBManager sharedInstance] executeQuery:selectSql];
}

@end
